package com.smrtcold.dev.utils;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;


/*
 * Copyright (C) DCIS 版权所有
 * 功能描述: TimeUtil 工具类,具类, 提供静态工具方法 操作时间
 * Create on MaQiang 2016-6-25 09:28:36
 */
public class TimeUtil {

    public static final ThreadLocal<SimpleDateFormat> YMDHMS = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
    public static final ThreadLocal<SimpleDateFormat> YMD = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
    public static final ThreadLocal<SimpleDateFormat> YM = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM");
        }
    };
    public static final ThreadLocal<SimpleDateFormat> YMD2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy.MM.dd");
        }
    };

    public static Long getLongtime() {
        return Long.parseLong(TimeUtil.getMillTime());
    }

    public static String now() {
        return TimeUtil.YMDHMS.get().format(new Date());
    }
    public static String now2() {
        return TimeUtil.YMD2.get().format(new Date());
    }

    public static String now(Date date) {
        return TimeUtil.YMDHMS.get().format(date);
    }


    public static String getYM() {
        return YM.get().format(new Date());
    }

    public static String getMillTime() {
        return (System.currentTimeMillis() + "").substring(0, 10);
    }


    //获得两个时间差(秒)
    public static long second(Date start, Date end) {
        return (end.getTime() - start.getTime()) / 1000;
    }



    //告警使用 @param time:时间  单位：s
    public static String toSecondTime(long time) {
        StringBuffer sb = new StringBuffer();
        Long day = time / 86400, sday = time % 86400, sm = sday % 3600, hour = sday / 3600, minute = sm / 60, second = sm % 60;
        if (day > 0) {
            sb.append(day).append("天");
        }
        if (hour > 0) {
            sb.append(hour).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分钟");
        }
        if (second > 0) {
            sb.append(second).append("秒");
        }
        return sb.toString();
    }

    //------------------------------------------------------------------------------------------------------------------------------------
    public static String getBeforeMinute(int MINUTE) {
        return getBeforeByTemp(Calendar.MINUTE, -MINUTE);
    }

    public static Date getHOUR( Date date, int HOUR) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, HOUR);
        return calendar.getTime();
    }
    public static Date getMINUTE( Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static String getBeforeHOUR(int HOUR) {
        return getBeforeByTemp(Calendar.HOUR, -HOUR);
    }

    public static String getTimeByDay(int DAY) {
        return getBeforeByTemp(Calendar.DAY_OF_MONTH, -DAY);
    }

    public static String getBeforeYMDDay(int DAY) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.DAY_OF_MONTH, -DAY);
        return YMD.get().format(beforeTime.getTime());
    }

    public static String getStartDay(int DAY) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.DAY_OF_MONTH, -DAY);
        return YMD.get().format(beforeTime.getTime()) + " 00:00:00";
    }

    public static String getEndDay(int DAY) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.DAY_OF_MONTH, -DAY);
        return YMD.get().format(beforeTime.getTime()) + " 23:59:59";
    }

    //
    public static String getYHMBeforeDay(int DAY) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.DAY_OF_MONTH, -DAY);
        return YMD.get().format(beforeTime.getTime());
    }

    private static String getBeforeByTemp(int type, int time) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(type, time);
        return YMDHMS.get().format(beforeTime.getTime());
    }

    //------------------------------------------------------------------------------------------------------------------------------------
    public static String getTimeByDay(String time, int DAY) {
        return getBefore(time, Calendar.DAY_OF_MONTH, DAY);
    }

    private static String getBefore(String time, Integer type, Integer size) {
        try {
            return getBefore(YMD.get().parse(time), type, size);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now();
    }

    private static String getBefore(Date time, Integer type, Integer size) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(type, size);
        return YMD.get().format(c.getTime());
    }

    //------------------------------------------------------------------------------------------------------------------------------------
    public static Date parseYHMHMS(String time) {
        try {
            return YMDHMS.get().parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date parseYHD(String time) {
        try {
            return YMD.get().parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }
    public static String parseYHD(Date time) {
        try {
            return YMD.get().format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date parseYHD2(String time) {
        try {
            return YMD2.get().parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }

    //
    public static Date stringToDate(String datetime) {
        try {
            return Utils.isNull(datetime) ? new Date() : YMDHMS.get().parse(datetime);
        } catch (Exception e) {
        }
        return new Date();
    }

    /**
     * 获取当前月第一天
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(Integer... month) {
        if(month.length==0)  month=new Integer[]{ Calendar.getInstance().get(Calendar.MONTH) + 1};
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month[0] - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime()) ;
    }

    /**
     * 获取指定月最后一天
     * @param month :指定的月
     * @return
     */
    public static String getLastDayOfMonth(Integer... month) {
        if(month.length==0)  month=new Integer[]{ Calendar.getInstance().get(Calendar.MONTH) + 1};
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,  month[0] - 1);
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH,  month[0]==2? calendar.getLeastMaximum(Calendar.DAY_OF_MONTH):calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(calendar.getTime());
    }





}